<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8" style="display: flex; align-items: center;">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
        >保存</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
        >查看已选数据</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-popconfirm
          title="确定需要转换"
          @confirm="handleChange"
        >
          <template slot="reference">
            <el-button
              type="primary"
              plain
              icon="el-icon-refresh"
              :disabled="!queryParams1.untransformed"
              size="mini"
              v-hasPermi="['mes:po:trans:edit']"
              >结案</el-button>
              <!-- @click="handleChange" -->
            </template>
        </el-popconfirm>
        <el-button
          type="primary"
          plain
          icon="el-icon-refresh"
          size="mini"
          style="margin-left: 20px;"
          @click="handleInput"
          v-hasPermi="['mes:po:trans:edit']"
          >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-radio v-model="queryParams1.untransformed" :label="false" @input="getList">已结案</el-radio>
        <el-radio v-model="queryParams1.untransformed" :label="true" @input="getList">未结案</el-radio>
      </el-col>
    </el-row>
    
    <div class="app-container-main">
      <!-- <dorpTable :tableData="tableData" :col="col" :height="430"/> -->
      <el-table :data="tableData" style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50" align="center" v-if="queryParams1.untransformed==true" />
        <el-table-column type="index" align="center" label="序号" width="50px" />
        <el-table-column show-overflow-tooltip prop="orderCode" label="销售订单号" />
        <el-table-column show-overflow-tooltip prop="orderTime" label="订单日期" >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="cremark" label="客户备注" />
        <el-table-column show-overflow-tooltip prop="platform" label="接单平台" />
        <el-table-column show-overflow-tooltip prop="orderCode" label="订单编号" />
        <el-table-column show-overflow-tooltip prop="clientName" label="客户名称"/>
        <el-table-column show-overflow-tooltip prop="proCode" label="产品编码"/>
        <el-table-column show-overflow-tooltip prop="proName" label="产品名称"/>
        <el-table-column show-overflow-tooltip prop="proSpecification" label="规格型号"/>
        <el-table-column show-overflow-tooltip prop="orderCount" label="订单数量" />
        <el-table-column show-overflow-tooltip prop="deadLine" label="订单交期" >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.deadLine, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="remark" label="备注"/>
        <el-table-column show-overflow-tooltip prop="price" label="单价"/>
        <el-table-column show-overflow-tooltip prop="totalPrice" label="金额"/>
        <!-- <el-table-column show-overflow-tooltip prop="orderStatus" label="订单进展">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.order_progress" :value="scope.row.orderStatus"/>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="deadLine" label="需求日期">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.deadLine, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="orderTime" label="制单日期" >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column> -->
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams1.pageNum"
        :limit.sync="queryParams1.pageSize"
        @pagination="getList"
      />
    </div>


    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的设备数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <!-- <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link> -->
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import dorpTable from '@/components/dorptable'
import { toWorkerOrder } from "@/api/mes/pro/orderToWorkOrder";
import { orderList, getChangeOrder } from "@/api/mes/pro/order";
import { getToken } from "@/utils/auth";
export default {
  name: 'OrderToWorkOrder',
  components: { dorpTable },
  dicts: ['order_progress', ],
  data() {
    return {
      radio: '2',
      queryParams1:{
        pageNum: 1,
        pageSize: 10,
        untransformed : true
      },
      total: 0,
      tableData: [],
      col:[
        {type: 'index', label: '序号', fixed: true, width: 50},
        {label: '订单编号', prop: 'orderCode'},
        {label: '订单名称', prop: 'orderName'},
        {label: '产品编码', prop: 'proCode'},
        {label: '产品名称', prop: 'proName'},
        {label: '产品规格', prop: 'proSpecification'},
        {label: '订单需求数', prop: 'orderCount'},
        {label: '单位', prop: 'unitOfMeasure'},
        {label: '已生产数量', prop: 'outputCount'},
        {label: '订单进展(状态)', prop: 'orderStatus'},
        {label: '需求日期', prop: 'deadLine'}, 
        {label: '制单日期', prop: 'orderTime'}, 
        {label: '客户编号', prop: 'customerNo'},
        {label: '客户名称', prop: 'customerName'},
        {label: '备注', prop: 'remark'},
      ],
      multipleSelection: [],
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/mes/pro/order/importPlanOrder"
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList(){
      getChangeOrder(this.queryParams1).then(res => {
        this.tableData = res.rows
        this.total = res.total
        console.log(res);
      })
      console.log('getList');
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(val);
    },
    handleChange(){
      if(this.multipleSelection.length === 0) return this.$message.warning('请选择要转换的订单')
      const ids = this.multipleSelection.map(item => item.id)
      toWorkerOrder({orderIds:ids}).then(res => {
        this.$message.success('转换成功')
        this.getList()
      })
    },
    handleInput(){
      this.upload.title = "数据导入";
      this.upload.open = true;
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
  }
}
</script>

<style lang="scss" scoped>

</style>